import Vue from 'vue'
import Router from 'vue-router'
import store from 'store'
import NProgress from 'nprogress' // Progress 进度条
import 'nprogress/nprogress.css'// Progress 进度条样式
import { Message } from 'element-ui'
import { getToken } from 'common/utils/' // 验权

const whiteList = ['/login'] // 不重定向白名单

// in development-env not use lazy-loading, because lazy-loading too many pages will cause webpack hot update too slow. so only in production use lazy-loading;
// detail: https://panjiachen.github.io/vue-element-admin-site/#/lazy-loading

Vue.use(Router)

/* Layout */
import Layout from 'views/layout/Layout'

/**
* hidden: true                   if `hidden:true` will not show in the sidebar(default is false)
* alwaysShow: true               if set true, will always show the root menu, whatever its child routes length
*                                if not set alwaysShow, only more than one route under the children
*                                it will becomes nested mode, otherwise not show the root menu
* redirect: noredirect           if `redirect:noredirect` will no redirct in the breadcrumb
* name:'router-name'             the name is used by <keep-alive> (must set!!!)
* meta : {
    title: 'title'               the name show in submenu and breadcrumb (recommend set)
    icon: 'svg-name'             the icon show in the sidebar,
  }
**/
export const constantRouterMap = [
  { path: '/login', name: '登录', component: () => import('views/login/index'), hidden: true },
  { path: '/404', name: '404', component: () => import('views/404'), hidden: true },
  { path: '/', redirect: '/home', hidden: true },
  {
    path: '/home',
    name: 'home',
    component: Layout,
    redirect: '/home/index',
    children: [{
      path: 'index',
      name: '首页',
      meta: { title: '首页', icon: 'table' },
      component: () => import('views/home/index')
    }]
  }
]

export const asyncRouterMap = [
  {
    path: '/person',
    name: 'person',
    component: Layout,
    children: [{
      path: 'index',
      name: '个人中心',
      meta: { title: '个人中心', icon: 'table' },
      component: () => import('views/person/index')
    }]
  },
  {
    path: '/super',
    name: '权限管理',
    meta: { roles: ['admin'], title: '权限管理', icon: 'example' },
    component: Layout,
    // redirect: '/super/list',
    children: [{
      path: 'index',
      name: '管理员列表',
      meta: { roles: ['admin'], title: '管理员列表', icon: 'table' },
      component: () => import('views/super/list/index')
    }]
  },
  {
    path: '/sort',
    name: 'sort',
    component: Layout,
    children: [{
      path: 'index',
      name: '分类',
      meta: { title: '分类', icon: 'table' },
      component: () => import('views/sort/index')
    }]
  },
  {
    path: '/tag',
    name: 'tag',
    component: Layout,
    children: [{
      path: 'index',
      name: '标签',
      meta: { title: '标签', icon: 'table' },
      component: () => import('views/tag/index')
    }]
  },
  {
    path: '/comment',
    name: 'comment',
    component: Layout,
    children: [{
      path: 'index',
      name: '评论',
      meta: { title: '评论', icon: 'table' },
      component: () => import('views/comment/index')
    }]
  },
  {
    path: '/article',
    name: '文章',
    meta: { title: '文章', icon: 'example' },
    component: Layout,
    // redirect: '/article/list',
    children: [{
      path: 'index',
      name: '文章列表',
      meta: { title: '文章列表', icon: 'documentation' },
      component: () => import('views/article/list/index')
    },
    {
      path: 'add',
      name: '添加文章',
      meta: { title: '添加文章', icon: 'documentation' },
      component: () => import('views/article/add/index')
    },
    {
      path: 'edit/:id',
      name: '编辑文章',
      hidden: true,
      meta: { title: '编辑文章', icon: 'documentation' },
      component: () => import('views/article/edit/index')
    }]
  },
  { path: '*', redirect: '/404', hidden: true }
]

const router = new Router({
  // mode: 'history', //后端支持可开
  // base: '/www/blog/admin/',
  scrollBehavior: () => ({ y: 0 }),
  routes: constantRouterMap
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  if (getToken()) {
    if (to.path === '/login') {
      next({ path: '/' })
      NProgress.done() // if current page is dashboard will not trigger	afterEach hook, so manually handle it
    } else {
      if (store.getters.roles.length === 0) {
        store.dispatch('GetInfo').then(res => { // 拉取用户信息
          const roles = res.data.roles // note: roles must be a array! such as: ['editor','develop']
          store.dispatch('GenerateRoutes', { roles }).then(() => { // 根据roles权限生成可访问的路由表
            router.addRoutes(store.getters.addRouters) // 动态添加可访问路由表
            next({ ...to, replace: true }) // hack方法 确保addRoutes已完成 ,set the replace: true so the navigation will not leave a history record
          })
        }).catch((err) => {
          store.dispatch('FedLogOut').then(() => {
            Message.error(err || 'Verification failed, please login again')
            next({ path: '/' })
          })
        })
      } else {
        next()
      }
    }
  } else {
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else {
      next('/login')
      NProgress.done()
    }
  }
})

router.afterEach((to, from) => {
  document.title = to.name + ' | 于梦中的博客后台'
  NProgress.done() // 结束Progress
})

export default router
